package org.example;

/**
 * 11. 盛最多水的容器
 */
public class MaxArea {

    /**
     * official
     */
    public int maxArea(int[] height) {
        int l = 0, r = height.length - 1;
        int ans = 0;
        while (l < r) {
            int area = Math.min(height[l], height[r]) * (r - l);
            ans = Math.max(ans, area);
            if (height[l] <= height[r]) {
                ++l;
            } else {
                --r;
            }
        }
        return ans;
    }

    public int maxAreaSelf(int[] height) {
        int length = height.length;
        if (length == 0 || length == 1) {
            return 0;
        }
        int max = 0;
        int maxHeight = height[0];
        for (int i = 0; i < length - 1; i++) {
            if (height[i] < maxHeight) {
                continue;
            } else {
                maxHeight = height[i];
            }
            for (int j = i + 1; j < length; j++) {
                int current = Math.min(height[i], height[j]) * (j - i);
                if (current > max) {
                    max = current;
                }
            }
        }
        return max;
    }

}
